<!--
+----------------------------------------------------------------------
| 友得云客  - 开启房产营销新纪元
+----------------------------------------------------------------------
| Copyright (c) 2019~2023 优得（西安）信息科技有限公司版权所有
+----------------------------------------------------------------------
| Licensed 友得云客不是自由软件 未经允许不可移除相关版权
+----------------------------------------------------------------------
| Author: www.youdeyunke.com
+----------------------------------------------------------------------
-->
<template>
    <el-dialog :visible.sync="show" width="600px" :destroy-on-close="true" :close-on-click-modal="false">
        <el-form size="small" label-position="right" label-width="110px" v-loading="loading">

            <el-form-item required label="卡券名称" label-position="left">
                <el-input style="width: 400px" v-model="coupon.name" required />
            </el-form-item>

            <el-form-item label="卡券简介">
                <el-input style="width: 400px" type="text" v-model="coupon.desc" required />
            </el-form-item>

            <el-form-item label="使用说明">
                <el-input style="width: 400px" type="textarea" :rows="8" placeholder="详细介绍卡券的使用规则、使用方式等"
                    v-model="coupon.content" required />
            </el-form-item>

            <el-form-item label="核销人手机号">
                <el-input show-word-limit style="width: 400px" type="text" placeholder="请输入11位手机号"
                    v-model="coupon.confirm_mobile" required />
                <info-tip tipText="多个手机号请用英文逗号分隔。只有此处填写的手机号，才能在小程序中进行扫码核销此卡券"></info-tip>
            </el-form-item>

            <el-form-item required label="生效时间" label-position="left">
                <el-date-picker style="width:200px;" v-model="coupon.starts_at" size="small" type="datetime"
                    format="yyyy-MM-dd HH:mm:ss" value-format="yyyy-MM-dd HH:mm:ss" placeholder="选择时间日期" />
            </el-form-item>

            <el-form-item required label="过期时间" label-position="left">
                <el-date-picker style="width:200px;" v-model="coupon.expired_at" size="small" type="datetime"
                    format="yyyy-MM-dd HH:mm:ss" value-format="yyyy-MM-dd HH:mm:ss" placeholder="选择时间日期" :picker-options="pickerOptions" />
            </el-form-item>

            <el-form-item required label="启用" label-position="left">
                <el-switch v-model="coupon.enable"></el-switch>
            </el-form-item>

        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button size="mini" :loading="loading" :disabled="loading" type="default" @click="cancleHandle" icon="el-icon-close">取消</el-button>
            <el-button size="mini" :loading="loading" :disabled="!canSubmit || loading" type="primary" @click="submitHandle" icon="el-icon-check">确定</el-button>
        </span>
    </el-dialog>
</template>

<style scoped>
.tips ul {
    padding: 0;
    margin: 0;
}

.tips {
    font-size: 12px;
    color: #999;
}

.show-more {
    display: flex;
    justify-content: flex-end;
    align-items: center;
    margin-top: 10px;
}

.icon-uploader {
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 35px;
    width: 100%;
    min-height: 100px;
    border: 2px dashed #f4f4f4;
}

.icon-uploader .el-upload {
    width: 100%;
    height: 100%;
}
</style>

<script>
import ImagePicker from "@/components/ImagePicker";
import DateTimePicker from "@/components/DateTimePicker";
import PostsSelector from "@/components/PostsSelector";
import { updateCouponConfig, createCouponConfig } from "@/api/coupon";
export default {
    name: "CouponConfigsForm",
    components: { ImagePicker, DateTimePicker },

    EnumerationSelectorprops: {},
    data () {
        return {
            coupon: {},
            show: false,
            loading: false,
            pickerOptions: {
        disabledDate(time) {
          return time.getTime() < Date.now(); // 禁用当前时间之前的日期
        }
      },
        };
    },
    computed: {
        canSubmit: function () {
            var h = this.coupon;
            if (h.desc && h.name && h.expired_at) {
                return true;
            }
            return false;
        },
    },
    watch: {},
    methods: {
        openDialog: function (item) {
            this.coupon = item;
            this.show = true;
        },

        cancleHandle: function () {
            this.show = false;
            this.coupon = {};
        },
        submitHandle: function () {
            var isok = this.validate(this.coupon);
            if (!isok) {
                return;
            }
            var isNew = !this.coupon.id;
            if (isNew) {
                return this.doCreate();
            }
            return this.doUpdate();
        },

        doCreate: function () {
            this.loading = true;
            createCouponConfig(this.coupon).then((resp) => {
                this.loading = false;
                if (resp.status == 0) {
                    this.$message.success("创建成功");
                    this.loading = false;
                    this.show = false;
                    this.$emit("change", {});
                }
            });
        },

        doUpdate: function () {
            var _this = this;
            this.loading = true;
            updateCouponConfig(this.coupon).then((resp) => {
                _this.loading = false;
                if (resp.status == 0) {
                    _this.$message.success("保存成功");
                    _this.$emit("change", {});
                    this.show = false;
                    _this.loading = false;
                }
            });
        },
        removeCover: function () {
            this.coupon.cover = "";
        },

        validate: function (data) {
            // TODO
            return true;
        },
    },
};
</script>
